package edu.sctu.zhuyingzhi.month201506.day20150623.HttpClientTest;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by user13 on 2015/6/23.
 */
public class ClientGetPostTest {

    static HttpClientBuilder builder = HttpClientBuilder.create();
    static CloseableHttpClient client = builder.build();

    /**
     * get方法
     * @throws IOException
     */
    public static void useGetRequests() throws IOException {
        HttpGet get = new HttpGet("http://localhost:8090/sctu/signin");
        HttpResponse response = client.execute(get);
        HttpEntity entity =response.getEntity();

        InputStream inputStream = entity.getContent();

        String str;
        str = steamToString(inputStream);
        System.out.println(str);

        get.releaseConnection();
    }

    /**
     * post方法
     */
    public static void usePostRequests(){
        HttpPost postUrl = new HttpPost("http://localhost:8090/sctu/signin");

        List<BasicNameValuePair> postMsg = new ArrayList<>();          //建立一个表单的集合前者是名称后者是内容
        postMsg.add(new BasicNameValuePair("username", "zhuyingzhi"));
        postMsg.add(new BasicNameValuePair("password", "13"));

        UrlEncodedFormEntity urlEncoded;

        try {
            urlEncoded = new UrlEncodedFormEntity(postMsg,"utf-8");
            postUrl.setEntity(urlEncoded);

            HttpResponse response = client.execute(postUrl);
            HttpEntity entity = response.getEntity();

            InputStream inputStream =entity.getContent();
            String str = steamToString(inputStream);

            System.out.println(str);
            postUrl.releaseConnection();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
/**
 * 上传文件                           未解决！！！！！！！！！！！！！！！！！！！！
 */

    public static void pushFile(){
        HttpPost post = new HttpPost("http://localhost:8090/sctu/file");

        List<BasicNameValuePair> fileMsg = new ArrayList<>();
        fileMsg.add(new BasicNameValuePair("file","D://haha.txt"));

        try {
            UrlEncodedFormEntity urlEncoded = new UrlEncodedFormEntity(fileMsg,"utf-8");
            post.setEntity(urlEncoded);


            HttpResponse response = client.execute(post);
            HttpEntity httpEntity = response.getEntity();


//            EntityUtils.consume(httpEntity);
            InputStream inputStream = httpEntity.getContent();
            System.out.println(steamToString(inputStream));
            post.releaseConnection();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    /**
     * 流变为字符串
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static String steamToString(InputStream inputStream) throws IOException {
        byte[] buffer = new  byte[1024];
        int count;
        String str = "";

        while((count = inputStream.read(buffer)) !=-1){
            str = str + new String(buffer,0,count,"utf-8");
        }
        return str;
    }
}
